package com.kongwg4.all.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.DbUtils;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author kongwg
 * @create 2021-01-10 11:26
 */
public class JDBCUtil {
    private static InputStream is = null;

    private static DataSource dataSource = null;

    static {
        try {
            is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc1.properties");
            Properties pros = new Properties();
            pros.load(is);
            dataSource = DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws SQLException {
        Connection conn = dataSource.getConnection();
        return conn;
    }


    public static void closeResource(Connection conn, PreparedStatement ps, ResultSet rs){
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);

    }
    public static void closeResource(Connection conn, PreparedStatement ps){
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);


    }
}
